// pages/serviceOrder/index.js
import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";
const { getUncompleteOrders, getCompleteOrders, addRemark, modifyOrder } = require('../../api/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabsActive: 0,
    tabViewSwiperActive: 0,
    uncompleteOrders: [],
    completeOrders: [],
    remarkPopupShow: false,
    remark: '',
    uncompleteOrdersCurrent: 1,
    completeOrdersCurrent: 1,
    baseConfig: {
      isAutoTriggered: false,
      shake: false, // 是否开启下拉震动
      height: 70, // 下拉高度
      text: {
        color: '#aaaaaa', // 文字颜色
        shadow: 5, // 是否开启shadow阴影,0为不开启,数值越大阴影范围越大
      },
    },
    loadMoreSetting: {
      status: 'more',
      more: {
        text: '上拉加载更多',
        color: '#999',
      },
      loading: {
        text: '加载中...',
        color: '#999',
      },
      noMore: {
        text: '-- 到底啦 --',
        color: '#999',
      },
    },
  },
  onTabsChange(event) {
    this.setData({
      tabViewSwiperActive: event.detail.name
    })
  },
  handleTabViewSwiperChange(event) {
    const { current, source } = event.detail
    this.setData({
      tabsActive: current
    })
  },
  handleCopyInfo(e) {
    const { info: data } = e.target.dataset;
    wx.setClipboardData({
      data,
      success(res) {

      }
    })
    const service = wx.getStorageSync('service-info')
    const { token: Authorization } = JSON.parse(service)
    const { index, id } = e.target.dataset
    const { uncompleteOrders } = this.data
    Dialog.confirm({
      title: '提醒',
      message: '确定要结束服务',
    })
      .then(async () => {
        // on confirm
        await modifyOrder({
          id,
          nodeStatus: 1// 订单状态(1:服务结束；2:退单)
        }, {
          header: {
            Authorization
          },
        })
        uncompleteOrders.splice(index, 1)
        this.setData({
          uncompleteOrders,
          tabViewSwiperActive: 1
        })
        this.handleCompleteOrdersRefresh()
      })
      .catch(() => {
        // on cancel
      });
  },
  handleCloseService(e) {
    const service = wx.getStorageSync('service-info')
    const { token: Authorization } = JSON.parse(service)
    const { index, id } = e.target.dataset
    const { uncompleteOrders } = this.data
    Dialog.confirm({
      title: '提醒',
      message: '确定要结束服务',
    })
      .then(async () => {
        // on confirm
        await modifyOrder({
          id,
          nodeStatus: 1// 订单状态(1:服务结束；2:退单)
        }, {
          header: {
            Authorization
          },
        })
        uncompleteOrders.splice(index, 1)
        this.setData({
          uncompleteOrders,
          tabViewSwiperActive: 1
        })
        this.handleCompleteOrdersRefresh()
      })
      .catch(() => {
        // on cancel
      });
  },
  handleChargeback(e) {
    const service = wx.getStorageSync('service-info')
    const { token: Authorization } = JSON.parse(service)
    const { index, id } = e.target.dataset
    const { uncompleteOrders } = this.data
    Dialog.confirm({
      title: '提醒',
      message: '确定要退单',
    })
      .then(async () => {
        // on confirm
        await modifyOrder({
          id,
          nodeStatus: 2// 订单状态(1:服务结束；2:退单)
        }, {
          header: {
            Authorization
          },
        })
        uncompleteOrders.splice(index, 1)
        this.setData({
          uncompleteOrders,
          tabViewSwiperActive: 1
        })
        this.handleCompleteOrdersRefresh()
      })
      .catch(() => {
        // on cancel
      });
  },
  handleRemarkPopupShow(e) {
    const { id, index, type } = e.target.dataset
    this.activeOrderInfo = {
      id, index, type
    }
    this.setData({
      remarkPopupShow: true,
      remark: ''
    })
  },
  handleRemarkPopupHide() {
    this.setData({
      remarkPopupShow: false,
      remark: ''
    })
  },
  async submitRemark() {
    const service = wx.getStorageSync('service-info')
    const { token: Authorization } = JSON.parse(service)
    const { id, index, type } = this.activeOrderInfo
    const { remark, uncompleteOrders, completeOrders } = this.data

    await addRemark({
      id,
      remark
    }, {
      header: {
        Authorization
      },
    })

    if (type === 'uncomplete') {
      uncompleteOrders[index].remark = remark
    } else {
      completeOrders[index].remark = remark
    }
    this.setData({
      uncompleteOrders,
      completeOrders
    })

    this.handleRemarkPopupHide()
    wx.showToast({
      icon: 'success',
      title: '添加成功',
    })
  },

  async fetchUncompleteOrders(reset = false) {
    const service = wx.getStorageSync('service-info')
    const { token: Authorization } = JSON.parse(service)
    const { uncompleteOrders, uncompleteOrdersCurrent } = this.data;
    const { data } = await getUncompleteOrders({ current: uncompleteOrdersCurrent, size: 10 }, {
      header: {
        Authorization
      },
    })
    if (Array.isArray(data) && data.length > 0) {
      this.setData({
        uncompleteOrdersCurrent: uncompleteOrdersCurrent + 1,
        uncompleteOrders: reset ? [...data] : [...uncompleteOrders, ...data]
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '没有数据了',
      })
    }
  },

  async handleUncompleteOrdersRefresh() {
    this.setData({
      uncompleteOrdersCurrent: 1,
    })
    await this.fetchUncompleteOrders(true)
    const scroller = this.selectComponent('.uncompleteOrders-scroller');
    scroller.settriggered(false)
  },

  async handleUncompleteOrdersLoadmore() {
    this.setData({
      'loadMoreSetting.status': 'loading'
    })
    await this.fetchUncompleteOrders()
    this.setData({
      'loadMoreSetting.status': 'more'
    })
  },

  async fetchCompleteOrders(reset = false) {
    const service = wx.getStorageSync('service-info')
    const { token: Authorization } = JSON.parse(service)
    const { completeOrders, completeOrdersCurrent } = this.data;
    const { data } = await getCompleteOrders({ current: completeOrdersCurrent, size: 10 }, {
      header: {
        Authorization
      },
    })
    if (Array.isArray(data) && data.length > 0) {
      this.setData({
        completeOrdersCurrent: completeOrdersCurrent + 1,
        completeOrders: reset ? [...data] : [...completeOrders, ...data]
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '没有数据了',
      })
    }
  },

  async handleCompleteOrdersRefresh() {
    this.setData({
      completeOrdersCurrent: 1,
    })
    await this.fetchCompleteOrders(true)
    const scroller = this.selectComponent('.completeOrders-scroller');
    scroller.settriggered(false)
  },
  async handleCompleteOrdersLoadmore() {
    this.setData({
      'loadMoreSetting.status': 'loading'
    })
    await this.fetchCompleteOrders()
    this.setData({
      'loadMoreSetting.status': 'more'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.fetchUncompleteOrders()
    this.fetchCompleteOrders()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})